const Controller = require("egg").Controller;
class UserController extends Controller {
    async info() {
        const ctx = this.ctx;
        const payload = ctx.request.query;
        const users = await ctx.service.user.find(payload);

        await this.ctx.render('users/index.nj', { list: users, search: payload });
        // ctx.body = user;
    }
    async create() {
        const { ctx, service } = this;
        const payload = ctx.request.body;
        try {
            const reslut = await service.user.create(payload);
            ctx.body = {
                code: 200,
                msg: "操作成功！"
            }
            ctx.redirect('/users')
        } catch (error) {
            ctx.body = {
                code: 500,
                mesg: "创建失败",
            };
        }
    }
    async delete() {
        const { ctx, service } = this;
        const { id } = ctx.request.query;
        try {
            const reslut = await service.user.delete(id);
            ctx.body = {
                code: 200,
                msg: "删除成功"
            }
            ctx.redirect('/users');
        } catch (error) {
            ctx.body = {
                code: 500,
                msg: "删除失败"
            }

        }



    }
    async update() {
        const { ctx, service } = this;
        const payload = ctx.request.body;
        try {
            const reslut = await service.user.update(payload);
            ctx.body = {
                code: 200,
                mesg: "更新成功"
            }
            ctx.redirect('/users');
        } catch (error) {

            ctx.body = {
                code: 500,
                mesg: error
            }
        }


    }



}



module.exports = UserController